package com.flowerplus.store;

import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import com.blankj.utilcode.util.Utils;
import com.flowerplus.store.common.data.http.HttpHelp;
import com.flowerplus.store.common.data.injector.component.ApplicationComponent;
import com.flowerplus.store.common.data.injector.component.DaggerApplicationComponent;
import com.flowerplus.store.common.data.injector.module.ApiModule;
import com.flowerplus.store.common.data.injector.module.ApplicationModule;
import com.flowerplus.store.common.data.utils.FileUtil;
import com.flowerplus.store.common.data.utils.upload.UploadFiles;
import com.flowerplus.store.common.structure.MainThreadHandler;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import javax.inject.Inject;


/**
 * Created by ems on 2017/2/27.
 * app 入口
 */

public class App extends MultiDexApplication {

    private static App app;
    private ApplicationComponent applicationComponent;



    @Inject
    UploadFiles mUploadFiles;
    @Inject
    HttpHelp mHttpHelp;

    @Override
    public void onCreate() {
        super.onCreate();
        this.app = this;
        init();

    }

    private void init() {
        initInjector();
        Logger.setContext(this);
        CrashHandler.getInstance().init(this, mUploadFiles);
////        FileUtil fileUtil = new FileUtil(this, new ResourcesUtil(this));
////        LoggerPrint.filePath(fileUtil.getPrintLogDir().getPath());
        MainThreadHandler.initLoop(getMainLooper());
        ZXingLibrary.initDisplayOpinion(this);
//        ContextUtility.setApplication(this);
        Utils.init(this);
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallback());
        MultiDex.install(this);
    }

//
//    private boolean initLeakCanary() {
//        if (LeakCanary.isInAnalyzerProcess(this)) {
//            return true;
//        }
//        LeakCanary.install(this);
//        return false;
//    }

    public void initInjector() {
        applicationComponent = DaggerApplicationComponent.builder().apiModule(new ApiModule())
                .applicationModule(new ApplicationModule(app.getApplicationContext()))
                .build();
        applicationComponent.inject(this);
    }

    public void initLogger() {
        Logger.config(new Logger.Builder()
                .setLog2FileSwitch(false)
                .setDir(FileUtil.getInstance().getLogDir())
                .setLevel(Logger.D));
    }

    public ApplicationComponent getApplicationComponent() {
        return applicationComponent;
    }

    public static App getApp() {
        return app;
    }


}
